<template>
  <view class="update-certificate">
    <view class="certificate-box">
      <text class="title"> 凭证上传规范样例 </text>
      <view class="certificate-img__box">
        <up-image class="img" src="https://cdn.uviewui.com/uview/album/1.jpg" />
        <view class="btn"> 查看样例 </view>
      </view>
    </view>

    <view class="certificate-box" @tap="handleUpdateCertificateBoxClick">
      <text class="title"> 凭证上传内容 </text>
      <view class="certificate-img__box">
        <up-image
          v-if="certificateImgUrl.length"
          class="img"
          :src="houseImgUrlPrefix + certificateImgUrl"
        />
        <up-icon v-else name="plus" :size="rpx2px(200)" />
        <view class="btn"> 上传凭证 </view>
      </view>
    </view>

    <view class="fixed-btn">
      <up-button
        class="btn__update-certificate"
        shape="circle"
        text="确认上传"
        color="linear-gradient(to right, #FF934C, #FC686F)"
        @tap="handleConfirmUpdateCertificateBtnClick"
      />
    </view>
  </view>
</template>
<script setup>
import { ref, inject } from "vue";
import { getUrlParamsInH5, getUrlParamsInWeixin, rpx2px } from "@/utils/utils";

import { userLeaseOrderPublicTransferApi } from "@/package_property/pages/api/life-services.js";
import { onLoad } from "@dcloudio/uni-app";
import { customNav } from "@/utils/customNav";
const houseImgUrlPrefix = inject("houseImgUrlPrefix");
const certificateImgUrl = ref("");
const orderId = ref("");
const handleConfirmUpdateCertificateBtnClick = () => {
  if (certificateImgUrl.value !== "") {
    switch (payName.value) {
      case "房屋订金":
        updateCertificateByReserve();
        break;
      case "房屋租金":
        updateCertificateByRent();
        break;
    }
  } else {
    uni.showToast({
      title: "请上传凭证",
      duration: 2000,
      icon: "none",
    });
  }
};
const updateCertificateByRent = async () => {
  let params = orderData.value;
  params.payType = "2";
  params.orderType = '1'
  params.paymentDocument = certificateImgUrl.value;
  console.log("上传参数", params);
  const res = await userLeaseOrderPublicTransferApi(params);
  const { code } = res;
  if (code === 200) {
    await uni.showToast({
      title: "提交成功",
      duration: 2000,
      success: () => {
        setTimeout(() => {
          customNav(`list`);
        }, 2000);
      },
    });
  }
};
const handleUpdateCertificateBoxClick = () => {
  uni.chooseImage({
    count: 1,
    sizeType: ["original", "compressed"],
    sourceType: ["album", "camera"],
    success: async (res) => {
      const { tempFilePaths } = res || {};
      const tempFile = tempFilePaths[0];
      uni.uploadFile({
        url: "/ht-smart-house/uploadFile/upload",
        filePath: tempFile,
        name: "file",
        success: (res) => {
          console.log("上传凭证", res);
          const { statusCode, data } = res;
          if (statusCode === 200) {
            const parseData = JSON.parse(data);
            certificateImgUrl.value = parseData.data;
          }
        },
      });
    },
  });
};
const payName = ref("");
const orderData = ref("");
onLoad(() => {
  /* #ifdef MP-WEIXIN */
  const option = JSON.parse(getUrlParamsInWeixin()?.orderData);
  orderData.value = option;
  orderId.value = option?.orderId;
  payName.value = getUrlParamsInWeixin()?.payName;
  /* #endif */

  /* #ifndef MP-WEIXIN */
  const option = JSON.parse(getUrlParamsInH5()?.orderData);
  orderData.value = option;
  orderId.value = option?.orderId;
  payName.value = getUrlParamsInWeixin()?.payName;
  /* #endif */
});
</script>
<style scoped lang=less>
@import "./style/update-certificate";
</style>
